#include <iostream>
#include "LinkStack.h"

bool IsOper(char c)
{
    switch (c)
    {
    case '+':
    case '-':
    case '*':
    case '/':
    case '(':
    case ')':
    case '=':
        return true;
    default:
        return false;
    }
}

bool IsNum(char c)
{
    if (c >= '0' && c <= '9')
        return true;
    return false;
}

int Operation(int prev, int next, char symbol)
{
    switch (symbol)
    {
    case '+':
        {return prev + next;
        break;}
    case '-':
        {return prev - next;
        break;}
    case '*':
        {return prev * next;
        break;}
    case '/':
        {if (next == 0)
        {
            printf("除以0！");
            exit(-1); // 退出程序
        }
        return prev / next;
        break;}
    default:
        exit(-1);
    }
}

char Precede(char ch1, char ch2)
{
    int row = 0, column = 0;
    switch (ch1)
    {
    case '+':
       { row = 0;
        break;}
    case '-':
        {row = 1;
        break;}
    case '*':
        {row = 2;
        break;}
    case '/':
        {row = 3;
        break;}
    case '(':
        {row = 4;
        break;}
    case ')':
        {row = 5;
        break;}
    case '=':
        {row = 6;
        break;}
    default:
        break;
    }

    switch (ch2)
    {
    case '+':
        {column = 0;
        break;}
    case '-':
        {column = 1;
        break;}
    case '*':
        {column = 2;
        break;}
    case '/':
        {column = 3;
        break;}
    case '(':
        {column = 4;
        break;}
    case ')':
        {column = 5;
        break;}
    case '=':
        {column = 6;
        break;}
    default:
        break;
    }

    char pre[7][7] =
        {
            {'>', '>', '<', '<', '<', '>', '>'},
            {'>', '>', '<', '<', '<', '>', '>'},
            {'>', '>', '>', '>', '<', '>', '>'},
            {'>', '>', '>', '>', '<', '>', '>'},
            {'<', '<', '<', '<', '<', '=', '0'},
            {'>', '>', '>', '>', '0', '>', '>'},
            {'<', '<', '<', '<', '<', '0', '='}
            };
    return pre[row][column];
}

int main()
{
    LinkStack NumS;  // 数据栈
    LinkStack OperS; // 运算符栈
    OperS.Push('=');
    char str[64] = {0};
    scanf("%[^\n]",str);//读到\n结束

    int i = 0;    
    while (str[i] != '\0')
    {
        if (IsNum(str[i]))
        {
            int num = 0;
            //i++;
            while (IsNum(str[i]))
            {
                num = 10 * num + str[i] - '0';
                i++;
            }
            std::cout << "num: " << num << std::endl;
            NumS.Push(num);
        }
        else if (IsOper(str[i]))
        {
            char pre = Precede(OperS.GetTop(), str[i]);
            switch (pre)
            {
            case '>':
            {
                int next = NumS.GetTop();
                NumS.Pop();
                int prev = NumS.GetTop();
                NumS.Pop();
                char op = OperS.GetTop();
                OperS.Pop();
                NumS.Push(Operation(prev, next, op));
                break;
            }
            case '<':
            {
                OperS.Push(str[i]);
                i++;
                break;
            }
            case '=':
            {
                OperS.Pop();//原本存的你那个无效符号也弹出
                i++;
                break;
            }
            default:
            {
                std::cout<<"error\n";
                exit(-1);
                break;
            }
                
            }
        }
        else if(str[i] == ' ')
        {
            while(str[i] == ' ')//过滤所有的空格
                i++;
        }
    }
    std::cout<<"answer is: "<<NumS.GetTop()<<std::endl;

    return 0;
}